package tools;

import Entity.goodBean;
import control.goodsControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

@WebServlet("/readGoodsServlet")
public class readGoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goods_id = request.getParameter("goods_id");
        String key = request.getParameter("key");
        String name = request.getParameter("name");
        HttpSession session=request.getSession();
        session.removeAttribute("goodslist");
        goodsControl.get_Goods(request,response);
        if(name==null) {
            System.out.println("获取当前商品的信息");
            try {
                ArrayList goodslist = null;
                if ((ArrayList) session.getAttribute("goodslist") != null) {
                    goodslist = (ArrayList) session.getAttribute("goodslist");
                    goodBean ff = new goodBean();
                    Iterator iter = goodslist.iterator();
                    while (iter.hasNext()) {
                        ff = (goodBean) iter.next();
                        if (ff.getGoods_id().equals(goods_id)) {
                            session.setAttribute("goodBean", ff);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (key.equals("0"))
                response.sendRedirect("http://localhost:8080/goods_content.jsp");
            else
                response.sendRedirect("http://localhost:8080/goods_alter.jsp");
        }
        else
        {
            name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
            System.out.println("检索商品"+name);

            ArrayList goodslist = (ArrayList) session.getAttribute("goodslist");
            System.out.println(goodslist);
            //http://localhost:8080/goodmanageServlet
            ArrayList goodslist2 = new ArrayList();
            goodBean ff = new goodBean();
            Iterator iter = goodslist.iterator();
            while (iter.hasNext()) {
                ff = (goodBean) iter.next();
                if (ff.getBid_type().contains(name) || ff.getName().contains(name) || ff.getGoods_price().contains(name)) {
                    goodslist2.add(ff);
                }
            }
            session.removeAttribute("goodslist");
            session.setAttribute("goodslist",goodslist2);
            response.sendRedirect("http://localhost:8080/goods_manage.jsp");
        }
        return;
    }
}
